package mes.kanban.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesPermission;
import mes.kanban.service.MesPermissionServiceI;

/**
 * shiro的辅助类
 * @author gjy
 *
 * 2018年2月12日下午3:47:19
 */
@Service
public class ShiroService {
	
	private static final Logger log = LoggerFactory.getLogger(ShiroService.class);

    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;
    
    @Autowired
    private Map<String, String> loadFilterChainDefinitions;
    
    @Autowired
    private MesPermissionServiceI mesPermissionServiceI;
    
    /**
     * 初始化权限
     */
    public Map<String, String> loadFilterChains() {
        // 权限控制map.从数据库获取
        Map<String, String> filterChainDefinitionMap = loadFilterChainDefinitions;
        List<MesPermission> resourcesList = mesPermissionServiceI.getPermissions();
        for(MesPermission resources:resourcesList){

            if (resources.getUrl()!=null) {
                String permission = "perms[" + resources.getUrl()+ "]";
                filterChainDefinitionMap.put(resources.getUrl(),permission);
            }
        }
        //这个是放在最后的
        filterChainDefinitionMap.put("/**", "authc");
        return filterChainDefinitionMap;
    }
    
    
	/**
     * 重新加载权限
     */
    public void updatePermission() {

        synchronized (shiroFilterFactoryBean) {

            AbstractShiroFilter shiroFilter = null;
            try {
                shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean
                        .getObject();
            } catch (Exception e) {
                throw new RuntimeException(
                        "get ShiroFilter from shiroFilterFactoryBean error!");
            }

            PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter
                    .getFilterChainResolver();
            DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver
                    .getFilterChainManager();

            // 清空老的权限控制
            manager.getFilterChains().clear();

            shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
            shiroFilterFactoryBean.setFilterChainDefinitionMap(loadFilterChains());
            // 重新构建生成
            Map<String, String> chains = shiroFilterFactoryBean
                    .getFilterChainDefinitionMap();
            for (Map.Entry<String, String> entry : chains.entrySet()) {
                String url = entry.getKey();
                String chainDefinition = entry.getValue().trim()
                        .replace(" ", "");
                manager.createChain(url, chainDefinition);
            }

           log.info("更新权限成功！！");
        }
    }

}
